<?php

/**
 * Base class for other PEAR classes.  Provides rudimentary
 * emulation of destructors.
 *
 * If you want a destructor in your class, inherit PEAR and make a
 * destructor method called _yourclassname (same name as the
 * constructor, but with a "_" prefix).  Also, in your constructor you
 * have to call the PEAR constructor: $this->PEAR();.
 * The destructor method will be called without parameters.  Note that
 * at in some SAPI implementations (such as Apache), any output during
 * the request shutdown (in which destructors are called) seems to be
 * discarded.  If you need to get any debug information from your
 * destructor, use error_log(), syslog() or something similar.
 *
 * IMPORTANT! To use the emulated destructors you need to create the
 * objects by reference: $obj =& new PEAR_child;
 *
 * @category   pear
 * @package    PEAR
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version    Release: 1.6.2
 * @link       http://pear.php.net/package/PEAR
 * @see        PEAR_Error
 * @since      Class available since PHP 4.0.2
 * @link        http://pear.php.net/manual/en/core.pear.php#core.pear.pear
 */
class PEAR{
	private $filename;
	private $debug;
	private $display;

	/*
	 * Constructor
	 * Arguments :$debug. If 1 then errors will be logged, else displayed
	 * */
	function __construct($filename, $debug = 0, $display = 0){
		$this->debug = $debug;
		$this->filename = $filename;
		$this->display = $display;

		//Report only user generated error messages
		error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

		if ($this->debug == 1){
			set_error_handler(array(
					$this,
					"ErrorHandler"
			));
		}else
			restore_error_handler();
	}

	/*
	 * Following function logs an error message
	 * */
	function ErrorHandler($errno, $errstr, $errfile, $errline){

		switch ($errno){
			case E_NOTICE:
			case E_USER_NOTICE:
				$error = 'Notice';
			break;
			case E_WARNING:
			case E_USER_WARNING:
				$error = 'Warning';
			break;
			case E_ERROR:
			case E_USER_ERROR:
				$error = 'Fatal Error';
			break;
			case E_DEPRECATED:
			case E_USER_DEPRECATED:
				$error = 'Deprecated';
			break;
			default:
				$error = 'Unknown (' . $errno . ')';
			break;
		}

		// prints debug messages on the screen
		if ($this->display == 1)
			$this->html($error . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);

		// Save to log file
		$this->writeLog('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
	}

	/*
	 * Save debug messages to log file
	 * */
	public function writeLog($message){
		$file = DIR_LOGS . $this->filename;

		$handle = fopen($file, 'a+');

		fwrite($handle, 'PTcms debug: ' . date('d/m/Y - G:i:s') . ' - ' . $message . "\n");

		fclose($handle);
	}

	/*
	 * Clear all debug messages on log file
	 * */
	public function clearLog(){
		$file = DIR_LOGS . $this->filename;

		$handle = fopen($file, 'w+');

		fclose($handle);
	}

	/*
	 * html for display
	 * */
	public function html($msg = ''){
		$str = '<br /><table border="0" cellspacing="0" cellpadding="2">';
		$str .= '<tr><td bgcolor="#f57900">';
		$str .= '<img src="' . HTTP_IMAGE_ADMIN . 'warning.png" width="32" height="32" alt="PTcms notice bug" style="vertical-align:middle; margin:5px">';
		$str .= '<font size="2" face="Verdana, Geneva, sans-serif"><b>' . $msg . '</b></font>';
		$str .= '</td></tr>';
		$str .= '<tr><td bgcolor="#e9b96e"><font face="verdana" size="1">PTcms notice bug in ' . date('d/m/Y - G:i:s') . ' - Memory usage: ' . $this->memory_usage() . '</font></td></tr>';
		$str .= '</table><br />';

		echo $str;
	}

	function memory_usage(){
		$mem_usage = memory_get_usage(true);

		if ($mem_usage < 1024)
			return $mem_usage . " bytes";
		elseif ($mem_usage < 1048576)
			return round($mem_usage / 1024, 2) . " kilobytes";
		else
			return round($mem_usage / 1048576, 2) . " megabytes";
	}
}